package edu.ccut.saturn.basecomponent.dataoperationcomponent;

import java.text.SimpleDateFormat;
import java.util.List;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameter;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;

public class FormatTime extends DefaultComponent {
	/**
	 * 
	 */
	private static final long serialVersionUID = -808644919964102396L;

	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		SaturnData  timeData = (SaturnData) parameterContext.getParameterValue(0);
		List<IParameter> list = parameterContext.getParameters();
		int len = (list.size()-1)/3*3;
		for(int i=1;i<len;i++){
			String key = (String) list.get(i++).getValue();
			String old_format = (String) list.get(i++).getValue();
			String format = (String) list.get(i).getValue();
			if(key!=null){
				String source = (String)timeData.get(key);
				SimpleDateFormat SDformat1 = new SimpleDateFormat(old_format);
				SimpleDateFormat SDformat2 = new SimpleDateFormat(format);
		    	String target = SDformat2.format(SDformat1.parse(source));
		    	timeData.set(key, target);
		    }		
		}
	}
}